package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.*;
import com.atguigu.service.*;
import com.github.pagehelper.PageInfo;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @PACKAGE_NAME: com.atguigu.controller
 * @CLASSNAME: HouseController
 * @AUTHOR: zhangsan
 * @DATE: 2024/4/22 15:24
 * @SINCE 17.0.7
 * @DESCRIPTION: HouseController 房源管理
 */
@Slf4j
@Controller
@RequestMapping("/house")
public class HouseController extends BaseController {

    private static final String PAGE_INDEX = "house/index";
    private static final String PAGE_CREATE = "house/create";
    private static final String PAGE_EDIT = "house/edit";
    /**
     * 房源详情
     */
    private static final String PAGE_SHOW = "house/show";
    private static final String LIST_ACTION = "redirect:/house";
    private static final String PAGE_SUCCESS = "common/successPage";
    @Reference
    private HouseService houseService;

    @Reference
    private CommunityService communityService;

    @Reference
    private HouseBrokerService houseBrokerService;

    @Reference
    private HouseUserService houseUserService;

    @Reference
    private HouseImageService houseImageService;

    @Reference
    private DictService dictService;

    @RequestMapping()
    public String index(Model model, HttpServletRequest request) {
        Map<String, Object> filters = getFilters(request);
        PageInfo<House> page = houseService.findPage(filters);

        getDictModel(model);

        model.addAttribute("filters",filters);
        model.addAttribute("page", page);

        return PAGE_INDEX;
    }

    @RequestMapping("/create")
    public String toCreatePage(Model model) {
        getDictModel(model);
        return PAGE_CREATE;
    }

    private void getDictModel(Model model) {
        //小区
        List<Community> communityList = communityService.findAll();
        //房屋类型
        List<Dict> houseTypeList = dictService.findListByDictCode("houseType");
        //楼层
        List<Dict> floorList = dictService.findListByDictCode("floor");
        //建筑结构
        List<Dict> buildStructureList = dictService.findListByDictCode("buildStructure");
        //装修情况
        List<Dict> decorationList = dictService.findListByDictCode("decoration");
        //朝向
        List<Dict> directionList = dictService.findListByDictCode("direction");
        //房屋用途
        List<Dict> houseUseList = dictService.findListByDictCode("houseUse");
        model.addAttribute("communityList",communityList);
        model.addAttribute("houseTypeList",houseTypeList);
        model.addAttribute("floorList",floorList);
        model.addAttribute("buildStructureList",buildStructureList);
        model.addAttribute("decorationList",decorationList);
        model.addAttribute("directionList",directionList);
        model.addAttribute("houseUseList",houseUseList);
    }

    @RequestMapping("/save")
    public String insertHouse(@NonNull House house) {
        house.setStatus(1);
        Integer i = houseService.insert(house);
        return PAGE_SUCCESS;
    }

    @RequestMapping("/edit/{id}")
    public String toEditPage(@NonNull Model model ,@NonNull @PathVariable("id") Long id) {
        House house = houseService.getById(id);
        getDictModel(model);
        model.addAttribute("house", house);
        return PAGE_EDIT;
    }

    @RequestMapping("/update")
    public String updateHouse(@NonNull House house) {
        Integer update = houseService.update(house);
        return PAGE_SUCCESS;
    }

    @RequestMapping("/delete/{id}")
    public String deleteHouse(@NonNull @PathVariable("id") Long id) {
        Integer delete = houseService.delete(id);
        return LIST_ACTION;
    }

    @RequestMapping("/publish/{id}/{status}")
    public String publish(@NonNull @PathVariable("id") Long id,
                          @NonNull @PathVariable("status") Integer status) {
        Integer i = houseService.updateHouseStatus(id, status);
        return LIST_ACTION;
    }

    @RequestMapping("/{id}")
    public String detail(@NonNull Model model, @NonNull @PathVariable("id") Long id) {
        //House house = houseService.getById(id);
        House house = houseService.getDictNameById(id);
        //Community community = communityService.getById(house.getCommunityId());
        Community community = communityService.getDictNameById(house.getCommunityId());
        List<HouseBroker> houseBrokerList = houseBrokerService.findListByHouseId(house.getId());
        List<HouseUser> houseUserList = houseUserService.findListByHouseId(house.getId());
        List<HouseImage> houseImage1List = houseImageService.findListByHouseIdAndType(house.getId(), 1);
        List<HouseImage> houseImage2List = houseImageService.findListByHouseIdAndType(house.getId(), 2);

        model.addAttribute("house", house);
        model.addAttribute("community", community);
        model.addAttribute("houseBrokerList", houseBrokerList);
        model.addAttribute("houseUserList", houseUserList);
        model.addAttribute("houseImage1List", houseImage1List);
        model.addAttribute("houseImage2List", houseImage2List);
        return PAGE_SHOW;
    }
}
